﻿using Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ViewModels.Contact;
using ViewModels.User;

namespace ProdService
{
    public class ContactService : BaseService
    {
        private readonly UserService _userService;
        public ContactService(DbContext dbContext, IHttpContextAccessor accessor, UserService userService)
            : base(dbContext, accessor)
        {
            _userService = userService;
        }

        public EditModel GetContact(int userId)
        {
            CurrentUserShouldBe(userId);
            Contact contact = dbContext.Set<User>().Find(userId).Contact;
            return mapper.Map<EditModel>(contact);
        }

        public void EditContact(int contactId, ShowModel model)
        {
            Contact contact = dbContext.Set<Contact>().Find(contactId);
            mapper.Map(model, contact);
        }

        public ShowModel ShowContact(int? id)
        {
            User user = id.HasValue ?
                dbContext.Set<User>().Find(id) :
                GetCurrentUser();
            if (user.Contact != null)
            {
                return mapper.Map<Contact, ShowModel>(user.Contact);
            }
            else
            {
                return null;
            }
        }

        public void ReportIssue(int contactId, string comment)
        {
            Contact contact = dbContext.Set<Contact>().Find(contactId);

            contact.Comment = comment;

            User user = dbContext.Set<User>().Where(u => u.Contact.Id == contactId)
                .SingleOrDefault();

            dbContext.SaveChanges();

        }
    }
}
